package com.ehomepay.escrow.transition.modules.monitor.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ehomepay.escrow.transition.common.base.BaseController;
import com.ehomepay.escrow.transition.common.redis.RedisService;
import com.ehomepay.escrow.transition.common.util.RandomCodeUtils;

@RestController
@RequestMapping("/monitor")
public class MonitorController extends BaseController {

	@Resource
	private RedisService redisService;
	
	/**
	 * 检测调用其他应用服务是否正常
	 * @return
	 */
	@RequestMapping(value = "/checkService", method = RequestMethod.GET)
	public Map<String, Object> checkService() {
		List<Map<String, String>> voList = new ArrayList<Map<String, String>>();
		Integer returnCode = 200;
		
		/**
		 * 校验redis服务
		 */
		Map<String, String> voMap = checkRedis();
		if(!voMap.isEmpty()){
			returnCode = 400;
			voList.add(voMap);
		}
		
		/**
		 * 返回
		 */
		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("code", returnCode);
		returnMap.put("data", voList);
		return returnMap;
	}
	
	/**
	 * 校验redis
	 * @return
	 */
	private Map<String, String> checkRedis(){
		Map<String, String> voMap = new HashMap<String, String>();
		String redisKey = RandomCodeUtils.getUUID();
		redisService.set(redisKey, redisKey);
		String redisValue = redisService.get(redisKey);
		if(StringUtils.isEmpty(redisValue)){
			voMap.put("400001", "数据库出错-redis");
		}
		redisService.delete(redisKey);
		return voMap;
	}
	
}
